// Update the actual stream.
function updateStream(){
    showStreamLoader();
    setTimeout(function(){
        var date = '';
        
        // If the filter was date, pass through the date.
        if(activeFilter === 'date'){
            date = $('#filterDateInput').val();
        }
        
        $.ajax({
            data: {
                'filter': activeFilter,
                'userId': userId,
                'date': date,
                'limit': limit
            },
            url: 'ajaj/readUserStream',
            type: 'POST',
            dataType: 'json',
            success: function(results){
                $('#stream').empty();
                
                console.log(results);
                
                if(results){
                    populateStream(results);
                }
                
                removeStreamLoader();
            }
        });
        
    }, streamTimerInterval);
}

// Populate function that will actually fill in the stream.
function populateStream(results){
	var indexCount = 0;
    
	$.each(results, function(index, a){
		var liClass = 'class="activity"';
	
		if(indexCount === 0){
			liClass = 'class="activity latestItem"';
		}
        
		var li = '<div id="activity_' + a.activityId + '"><li ' + liClass + '>';
		li += '<div class="picture">';
			li += '<img src="https://graph.facebook.com/' + a.userId + '/picture" alt="' + ucwords(a.userName) + '" />';
		li += '</div>';
        li += '<table class="content">';
			li += '<tr>';
            
                var title = '<a href="profile/user/' + a.userId + '">' + ucwords(a.userName) + '</a>';
                
                if(a.withWho !== undefined){
                    var withWhoLength = a.withWho.length;
                    var counter = 1;
                    
                    $.each(a.withWho, function(i, ip){
                        
                        var userLink = '<a href="profile/user/' + ip.userId + '">' + ucwords(ip.userName) + '</a>';
                        
                        if(counter === withWhoLength){
                            title += ' en ' + userLink;
                        } else {
                            title += ', ' + userLink;
                        }
                        counter++;
                    });
                }
                
                li += '<td class="title">' + title + '</td>';
			li += '</tr>';
			li += '<tr>';
            
                var message = '';
            
                if(a.messageHelper[1] !== undefined){
                    message = ucwords(a.messageHelper[0]) + ' ' + a.messageHelper[1];
                   
                } else {
                    message = ucwords(a.messageHelper[0]) + ' ' + ucwords(a.messageHelper[2]);
                }
                
                if(a.time !== null){
                    message += ' om ' + a.time.substr(0, 5);
                }
                
                if(a.endDate !== null){
                    if(a.messageHelper[3] !== undefined){
                        message += ' tot ' + a.messageHelper[3];
                    } else if(a.messageHelper[4] !== undefined){
                        message += ' tot ' + ucwords(a.messageHelper[4]);
                        
                        if(a.endTime !== null){
                            message += ' om ' + a.endTime.substr(0, 5);
                        }
                    } else {
                        if(a.endTime !== null){
                            message += ' tot ' + a.endTime.substr(0, 5);
                        }
                    }
                }
                
                message += ' naar';
                
                // Display the object information (destination).
                if(a.destType === '1'){
                    message += ' <a href="profile/company/' + a.object.id + '">' + ucwords(a.object.name) + '</a>';
                } else if(a.destType === '2'){
                    message += ' <a href="profile/user/' + a.object.id + '">' + ucwords(a.object.name) + '</a>';
                } else if(a.destType === '3'){
                    message += ' <a href="profile/city/' + a.object.id + '">' + ucwords(a.object.name) + '</a>';
                }

                 li += '<td class="message">' + message + '</td>';                
			li += '</tr>';
			li += '<tr>';
                 li += '<td><abbr class="timeago" id="timeago_' + a.activityId + '"></td>';
			li += '</tr>';
		li += '</table>';
		
		li += '<div class="controls">';
			li += '<div class="normal">';
            
                // Show the tag of the activity.
				if(a.tagId !== null){
					li += '<div><a href="' + url + 'profile/tag/' + a.tagId + '">' + ucwords(a.title) + '</a></div>'; 
				}
			
			li += '</div>';
		li += '</div>';
		li += '<div class="clear"></div>';
		li += '</li></div>';
		
		li = $(li);
		li.appendTo('#stream');
        
        // Posted x minutes ago etc.
        var t = a.createdOn.split(/[- :]/);
        var d = new Date(t[0], t[1]-1, t[2], t[3], t[4], t[5]);
        
        $('#timeago_' + a.activityId).attr("title", iso8601(d));
        
		indexCount++;
	});
    
     $('.timeago').timeago();
}